Skip to content

bin(num). convert ZERO and negative decimal numbers to binary. #1093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 3, 2019
Merged

bin(num). convert ZERO and negative decimal numbers to binary. #1093

merged 7 commits into from
Aug 3, 2019

Conversation

syedwaleedhyder
Copy link
Contributor

@syedwaleedhyder syedwaleedhyder commented Aug 2, 2019

Fixes #1090

bin(num) in decimal_to_binary.py can now convert ZERO and negative decimal numbers to binary. Consistent with built-in python bin(x) function.

@cclauss
Copy link
Member

cclauss commented Aug 2, 2019

No doctests?? :-(

@PatOnTheBack
Copy link
Contributor

@syedwaleedhyder you can find information about writing doctests on Python's website.

…s to binary. Consistent with built-in python bin(x) function.
@syedwaleedhyder
Copy link
Contributor Author

Doctests added :) @cclauss @PatOnTheBack

@PatOnTheBack
Copy link
Contributor

@syedwaleedhyder why are you testing decimal_to_hexadecimal?

…s to binary. Consistent with built-in python bin(x) function.
@syedwaleedhyder
Copy link
Contributor Author

@PatOnTheBack Corrected.

Copy link
Contributor

@PatOnTheBack PatOnTheBack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@syedwaleedhyder
Copy link
Contributor Author

@PatOnTheBack How much time LGTM takes?

Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python3 -m doctest -v decimal_to_binary.py finds no doctests. They need to be in the function's first docstring, not in the second. When I get them to run, 6 pass and 3 fail.

…s to binary. Consistent with built-in python bin(x) function.
@syedwaleedhyder
Copy link
Contributor Author

@cclauss Resolved. All test cases pass now.

@@ -35,9 +34,9 @@ def decimal_to_binary(num):
True
"""
assert type(num) in (int, float) and num == int(num)

num = int(num)
Copy link
Member

@cclauss cclauss Aug 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following test fails...

>>> decimal_to_binary(2.0) == bin(2.0)
True

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cclauss bin(2.0) raises exception. And my code also raises exception. But the test still fails. Any idea how to resolve this issue? Can I just hard code the error in the doctests?
`**********************************************************************
File "decimal_to_binary.py", line 34, in main.decimal_to_binary
Failed example:
decimal_to_binary(2.0) == bin(2.0) # doctest: +ELLIPSIS
Exception raised:
Traceback (most recent call last):
File "/usr/lib/python3.6/doctest.py", line 1330, in __run
compileflags, 1), test.globs)
File "<doctest main.decimal_to_binary[9]>", line 1, in
decimal_to_binary(2.0) == bin(2.0) # doctest: +ELLIPSIS
File "decimal_to_binary.py", line 38, in decimal_to_binary
raise TypeError("'float' object cannot be interpreted as an integer")
TypeError: 'float' object cannot be interpreted as an integer


`

@cclauss
Copy link
Member

cclauss commented Aug 3, 2019

# doctest: +ELLIPSIS as is done at

>>> prime_sum(-7) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
IndexError: list assignment index out of range
>>> prime_sum("seven") # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: can only concatenate str (not "int") to str

@cclauss
Copy link
Member

cclauss commented Aug 3, 2019

The exceptions do not have to be en exact match as long as the exception type and exception string are the same.

@cclauss
Copy link
Member

cclauss commented Aug 3, 2019

Are we ready to merge?

@syedwaleedhyder
Copy link
Contributor Author

I think so. Still have a final look @cclauss .

@syedwaleedhyder
Copy link
Contributor Author

Trying: decimal_to_binary(0) Expecting: '0b0' ok Trying: decimal_to_binary(2) Expecting: '0b10' ok Trying: decimal_to_binary(7) Expecting: '0b111' ok Trying: decimal_to_binary(35) Expecting: '0b100011' ok Trying: decimal_to_binary(-2) Expecting: '-0b10' ok Trying: decimal_to_binary(16.16) # doctest: +ELLIPSIS Expecting: Traceback (most recent call last): ... TypeError: 'float' object cannot be interpreted as an integer ok Trying: decimal_to_binary('0xfffff') # doctest: +ELLIPSIS Expecting: Traceback (most recent call last): ... TypeError: 'str' object cannot be interpreted as an integer ok 1 items had no tests: decimal_to_binary 1 items passed all tests: 7 tests in decimal_to_binary.decimal_to_binary 7 tests in 2 items. 7 passed and 0 failed. Test passed.

@cclauss cclauss merged commit e313141 into TheAlgorithms:master Aug 3, 2019
@cclauss
Copy link
Member

cclauss commented Aug 3, 2019

Thanks for your patience and persistence!

@syedwaleedhyder
Copy link
Contributor Author

Liked to work with you. Thanks. @cclauss

stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
…gorithms#1093)

* bin(num) can convert ZERO and negative decimal numbers to binary. Consistent with built-in python bin(x) function.

* bin(num) can convert ZERO and negative decimal numbers to binary. Consistent with built-in python bin(x) function.

* Added doctests. bin(num) can convert ZERO and negative decimal numbers to binary. Consistent with built-in python bin(x) function.

* Added doctests. bin(num) can convert ZERO and negative decimal numbers to binary. Consistent with built-in python bin(x) function.

* Added doctests. bin(num) can convert ZERO and negative decimal numbers to binary. Consistent with built-in python bin(x) function.

* doctests still failing.

* Doctests added.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

decimal_to_binary() should return identical values as bin()
3 participants